package com.moli.iov.protocol.model;


/*
 * 文件名： RealTimeInfo.java
 * 
 * 创建日期： 2016年11月30日
 *
 *
 */

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;

import com.moli.iov.protocol.codec.DataEncoder;
import com.moli.iov.protocol.codec.Serialize;
import com.moli.iov.protocol.enums.RealTimeInfoTypeTag;
import com.moli.iov.protocol.enums.realtime.ChargeStateTag;
import com.moli.iov.protocol.enums.realtime.DcStateTag;
import com.moli.iov.protocol.enums.realtime.GearStateTag;
import com.moli.iov.protocol.enums.realtime.LocationStateTag;
import com.moli.iov.protocol.enums.realtime.RunModeTag;
import com.moli.iov.protocol.enums.realtime.VehicleStateTag;
import com.moli.iov.protocol.model.realtime.VehicleAllReq;
import com.moli.iov.protocol.model.realtime.VehicleGpsReq;
import com.moli.iov.util.ByteArrayUtil;


/**
 * 补发信息上报 实体
 *
 * @version $Revision$
 *
 * @since 2016年11月30日
 */
@Serialize(unit = "byte", isBigEndian = true)
public class ReSendInfoReq implements Serializable {

	/** 
	*/
	public static final long serialVersionUID = 1L;

	/**
	 * 数据采集时间
	 */
	@Serialize(unit = "byte", offset = 0, size = 6)
	public Date collectDate;

	/**
	 * 数据消息字节序
	 */
	@Serialize(unit = "byte", offset = 6, size = -1)
	public byte[] infoDatas;

	/**
	 * 添加消息解析完成后的实体
	 * 
	 * @param infoType
	 * @param obj
	 */
	public void addInfo(RealTimeInfoTypeTag infoType, byte[] infoBytes) {
		if (infoType == null || infoBytes == null || infoBytes.length <= 0)
			return;
		// 获取添加的消息数组长度
		int infoLen = infoBytes.length;
		// 消息类型+消息体 数组长度
		int typeAndInfoLen = infoLen + 1;
		// 定义临时字节数组存储 消息类型byte与消息数组byte[]
		byte[] tmpTypeAndInfoBytes = new byte[typeAndInfoLen];
		// 获取添加的消息类型的 byte
		tmpTypeAndInfoBytes[0] = infoType.getV();
		// 拷贝数组到临时数组变量
		System.arraycopy(infoBytes, 0, tmpTypeAndInfoBytes, 1, infoLen);

		// 设置当前上报信息字节数组
		if (this.infoDatas == null) {
			this.infoDatas = new byte[typeAndInfoLen];
			// 拷贝临时变量到上报信息 总字节序中
			System.arraycopy(tmpTypeAndInfoBytes, 0, infoDatas, 0, typeAndInfoLen);
		}
		else {
			// 拷贝临时变量到上报信息 总字节序中
			this.infoDatas = Arrays.copyOf(this.infoDatas, typeAndInfoLen + infoDatas.length);
			System.arraycopy(tmpTypeAndInfoBytes, 0, infoDatas, infoDatas.length - typeAndInfoLen, typeAndInfoLen);
		}

	}

	public void addInfo(RealTimeInfoTypeTag infoType, Object infoObj) throws Exception {
		if (infoType == null || infoObj == null)
			return;
		DataEncoder<Object> encoder = new DataEncoder<Object>();
		byte[] infoBytes = encoder.encode(infoObj);
		addInfo(infoType, infoBytes);

	}

	public Date getCollectDate() {
		return collectDate;
	}

	public void setCollectDate(Date collectDate) {
		this.collectDate = collectDate;
	}

	public byte[] getInfoDatas() {
		return infoDatas;
	}

	public void setInfoDatas(byte[] infoDatas) {
		this.infoDatas = infoDatas;
	}

	// public LinkedHashMap<InfoTypeTag, byte[]> getInfoMap() {
	// return infoMap;
	// }
	//
	// public void setInfoMap(LinkedHashMap<InfoTypeTag, byte[]> infoMap) {
	// this.infoMap = infoMap;
	// }

	public static void main(String[] args) {
		try {

			VehicleAllReq v = new VehicleAllReq();
			v.setVehicleState(VehicleStateTag.STARTING);
			v.setChargeState(ChargeStateTag.TRAVEL_CHARGING);
			v.setRunMode(RunModeTag.EV);
			v.setSpeed(120);
			v.setOdometer(1000000);
			v.setTotalVoltage((short) 10000);
			v.setTotalCurrent((short) 12222);
			v.setSoc((byte) 56);
			v.setDcStateTag(DcStateTag.WORKING);
			v.setGear(new GearStateTag(GearStateTag.ExistDriveForces.YES, GearStateTag.ExistBraking.YES,
					GearStateTag.Gears.DRIVE));
			v.setInsResistance((short) 1344);

			// v.setReserves(reserves);

			// ReportHeader rh = new ReportHeader();
			// rh.setCommandTag(CommandTag.VEHICLE_LOGIN);
			// rh.setResponseTag(ResponseTag.IS_COMMAND);
			// rh.setUid("1");
			// rh.setEncryptTag(EncryptTag.AES128_ENCRYPT);
			DataEncoder<VehicleAllReq> vDataEncoder = new DataEncoder<VehicleAllReq>();
			byte[] bytes = vDataEncoder.encode(v);

			// ReportBody<VehicleAllData> vData = new
			// ReportBody<VehicleAllData>(rh,
			// v);
			System.out.println("VehicleAllData-->" + (ByteArrayUtil.toHexString(bytes)));

			VehicleGpsReq vlData = new VehicleGpsReq();
			vlData.setLocationState(new LocationStateTag(LocationStateTag.IsValid.NO, LocationStateTag.LAT.SOUTH,
					LocationStateTag.LON.WEST));
			vlData.setLatitude(155778789);
			vlData.setLongitude(28987654);
			DataEncoder<VehicleGpsReq> vLocationEncoder = new DataEncoder<VehicleGpsReq>();
			byte[] locationBytes = vLocationEncoder.encode(vlData);
			// ReportBody<VehicleAllData> vData = new
			// ReportBody<VehicleAllData>(rh,
			// v);
			System.out.println(("VehicleLocationData-->" + ByteArrayUtil.toHexString(locationBytes)));

			ReSendInfoReq reSendInfoData = new ReSendInfoReq();
			reSendInfoData.setCollectDate(new Date());
			reSendInfoData.addInfo(RealTimeInfoTypeTag.VEHICLE_LOCATION_DATA, locationBytes);
			reSendInfoData.addInfo(RealTimeInfoTypeTag.VEHICLE_DATA, bytes);

			DataEncoder<ReSendInfoReq> vDecoder = new DataEncoder<ReSendInfoReq>();
			byte[] bytes1 = vDecoder.encode(reSendInfoData);
			System.out.println(("RealTimeInfoData-->" + ByteArrayUtil.toHexString(bytes1)));
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
